﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainModel;
using System.Data;
using BIZ;
using DAL;
using DAL.Service;
using DAL.Service.Impl;
using System.Collections;
using System.Web.Script.Serialization;

namespace JoyWeb.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        UserOper uo = new UserOper();
        IUserInfoService userService = new UserInfoServiceImpl();
        public ActionResult Login()
        {
            return View();
        }


        public JsonResult userlogin()
        {
            string uname = (Request["uname"] == null ? "" : Request["uname"].ToString());
            string upass = (Request["upass"] == null ? "" : Request["upass"].ToString());
            UserInfo user = new UserInfo();
            int returnvalue;
            string ip = Request.UserHostAddress;
            if (uname == "")
                return Json((int)DAL.Enums.loginState.用户名为空);
            if (upass == "")
                return Json((int)DAL.Enums.loginState.密码为空);
            //判断是否已经登录
            ControllerContext cxt = this.ControllerContext;
            DataTable dt = (DataTable)cxt.HttpContext.Application["UserOnline"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                if (dr["username"].ToString() == uname)//如果已经登录
                {
                    if (dr["userip"].ToString() == ip)
                    {
                        returnvalue = uo.userlogin(uname, upass, ip, (int)DAL.Enums.userloginfrom.网站, ref user);
                        if ((int)DAL.Enums.loginState.登录成功 == returnvalue)
                        {
                            Session["user"] = user;
                            dr["lasttime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                            cxt.HttpContext.Application.Lock();
                            cxt.HttpContext.Application["UserOnline"] = dt;
                            cxt.HttpContext.Application.UnLock();
                        }
                        return Json(returnvalue);
                    }
                    else//如果别点登录判断时间是否过期
                    {
                        if (DateTime.Compare(DateTime.Parse(dr["lasttime"].ToString()).AddMinutes(30), DateTime.Now) <= 0)
                        {
                            //登录流程
                            returnvalue = uo.userlogin(uname, upass, ip, (int)DAL.Enums.userloginfrom.网站, ref user);
                            if ((int)DAL.Enums.loginState.登录成功 == returnvalue)
                            {
                                Session["user"] = user;
                                dr["lasttime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                                dr["userip"] = ip;
                                cxt.HttpContext.Application.Lock();
                                cxt.HttpContext.Application["UserOnline"] = dt;
                                cxt.HttpContext.Application.UnLock();
                            }
                            return Json(returnvalue);
                        }
                        else
                        {
                            return Json((int)DAL.Enums.loginState.帐号已在别处登录);
                        }
                    }
                }
            }

            //如果第一次登录
            //登录流程
            returnvalue = uo.userlogin(uname, upass, ip, (int)DAL.Enums.userloginfrom.网站, ref user);
            if ((int)DAL.Enums.loginState.登录成功 == returnvalue)
            {
                Session["user"] = user;
                DataRow dr = dt.NewRow();
                dr["lasttime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                dr["userip"] = ip;
                dr["username"] = user.u_nick;
                dt.Rows.Add(dr);
                cxt.HttpContext.Application.Lock();
                cxt.HttpContext.Application["UserOnline"] = dt;
                cxt.HttpContext.Application.UnLock();
            }
            return Json(returnvalue);
        }
        public ActionResult loginout()
        {
            ControllerContext cxt = this.ControllerContext;
            DataTable dt = (DataTable)cxt.HttpContext.Application["UserOnline"];
            UserInfo user = (UserInfo)Session["user"];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                if (dr["username"].ToString() == user.u_nick)
                {
                    dt.Rows.Remove(dr);
                    cxt.HttpContext.Application.Lock();
                    cxt.HttpContext.Application["UserOnline"] = dt;
                    cxt.HttpContext.Application.UnLock();
                    //写登出日志 
                    uo.saveLoginLog(user.u_id, Request.UserHostAddress, (int)DAL.Enums.useropertype.登出, null, (int)DAL.Enums.userloginfrom.网站);
                    Session.Remove("user");
                    Session.Remove("car");//清空借书车的session
                    break;
                }
            }
            return RedirectToAction("Index", "Index");
        }

    }
}
